import Vue from 'vue'
import Vuex from 'vuex'
import { setStorage, getStorage } from '@/utils/storage'

Vue.use(Vuex)

export default new Vuex.Store({
  state: { // 把获取到的token和refresh_token保存在全局
    token: getStorage('token') || '',
    refresh_token: getStorage('refresh_token') || '',
    user: {}
  },
  mutations: { // 只有mutations里能修改state的数据
    setToken (state, token) {
      state.token = token// 把获取到的token和refresh_token保存在全局
      setStorage('token', token)// 把获取到的token和refresh_token保存在本地（浏览器）下面同理，因为光存在vuex中，页面刷新，vuex数据就会回归初始化，保存在本地，等调接口的时候，vuex就会从本地把token取出来
    },
    setRefreshToken (state, refresh_token) {
      state.refresh_token = refresh_token
      setStorage('refresh_token', refresh_token)
    },
    setUser (state, obj) {
      state.user = obj
    },
    // 更新头像
    updatePhoto (state, url) {
      state.user.photo = url
    },
    // 更新姓名
    updateName (state, theName) {
      state.user.name = theName
    }
  },
  actions: {
  },
  modules: {
  }
})
